package com.zbvc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zbvc.dao.BookTypeDao;
import com.zbvc.entity.BookType;
import com.zbvc.util.DBUtil;

public class BookTypeDaoImpl implements BookTypeDao {

	@Override
	public List<BookType> findBookTypeList() {
		String sql = "SELECT * FROM booktype ";
		Connection conn = DBUtil.getConnect();
		PreparedStatement pstm = null;
		ResultSet rs = null;
		List<BookType> lists = new ArrayList<BookType>();
		try {
			pstm = conn.prepareStatement(sql);
			rs = pstm.executeQuery();
			while(rs.next()) {
				BookType type = new BookType();
				type.setTid(rs.getInt("tid"));
				type.setName(rs.getString("name"));
				lists.add(type);
			}
			return lists;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return null;
	}
	/**
	 * 修改分类
	 */
	@Override
	public int updateTypeById(int tid, String name) {
		Connection conn = DBUtil.getConnect();
		String sql = "UPDATE booktype SET name=? WHERE tid=?";
		try {
			PreparedStatement pstm = conn.prepareStatement(sql);
			pstm.setString(1, name);
			pstm.setInt(2, tid);
			int row = pstm.executeUpdate();
			return row;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	/**
	 * 增加分类
	 */
	@Override
	public int insertBookType(String name) {
		Connection conn = DBUtil.getConnect();
		String sql = "INSERT INTO booktype (name) VALUES (?)";
		try {
			PreparedStatement pstm = conn.prepareStatement(sql);
			pstm.setString(1, name);
			int row = pstm.executeUpdate();
			return row;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

}
